//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
#include <errno.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <wchar.h>
#include <string.h>

#include <aura.h>

EXTERN_C void AnsiToUnic(
            /* [out] */ Aura_PWChar desc,
            /* [in] */ char *src)
{
    while (*src) {
        *desc++ = (Aura_WChar)*src++;
    }
    *desc = 0;
}

int main(int argc, char argv[])
{
    Aura_WChar wImageName[64];
    Aura_WChar wArguments[64];
    Aura_Handle hProcess = NULL;
    Aura_ProcessId iProcessId = 0;
    Aura_ProcessId CurrentProcessId = 0;
    Aura_ECode ec;
    Aura_Millisecond pUserTime;
    Aura_Millisecond pPrivilegedTime;

    AnsiToUnic(wImageName, "hello.exe");
    AnsiToUnic(wArguments, "");

    AuraCentralContext_Set((Aura_Address)0);

    ec = AuraProcess_Create(wImageName, wArguments,
        NULL, &iProcessId);

    //getchar();

    printf("In aura process.\n");

    AuraProcess_GetCurrentId(&CurrentProcessId);
    printf("CurrentProcessId:%d\n", CurrentProcessId);

    AuraProcess_GetProcessorTime(NULL, &pUserTime, &pPrivilegedTime);
    printf("CurrentProcess pUserTime:%d\n", pUserTime);
    printf("CurrentProcess pPrivilegedTime:%d\n", pPrivilegedTime);

    printf("Get the child process info\n");
    AuraProcess_Open(iProcessId, &hProcess);

    Aura_Int32 PrivateVirtualMemorySize;
    Aura_Int32 SharedVirtualMemorySize;
    Aura_Int32 PrivatePhysicalMemorySize;
    Aura_Int32 SharedPhysicalMemorySize;

    ec = AuraProcess_GetMemoryInformation(
    hProcess,
    &PrivateVirtualMemorySize,
    &SharedVirtualMemorySize,
    &PrivatePhysicalMemorySize,
    &SharedPhysicalMemorySize);
    if(AURA_FAILED(ec)){
        printf("Error: AuraProcess_GetMemoryInformation failed! \
        file:%s. line:%d. \n",
        __FILE__, __LINE__);
    }

    printf("PrivateVirtualMemorySize:%d.\n", PrivateVirtualMemorySize);
    printf("SharedVirtualMemorySize:%d.\n", SharedVirtualMemorySize);
    printf("PrivatePhysicalMemorySize:%d.\n", PrivatePhysicalMemorySize);
    printf("SharedPhysicalMemorySize:%d.\n", SharedPhysicalMemorySize);

Aura_ThreadPriorityRank nPrioriyRank;
ec = AuraProcess_GetPriorityRank(hProcess, &nPrioriyRank);
if(AURA_FAILED(ec))
{
printf("Error: AuraProcess_GetPriorityRank failed! \
        file:%s. line:%d. \n",
        __FILE__, __LINE__);
}
printf("Process Priority Rank: d.\n",nPrioriyRank);
ec = AuraProcess_SetPriorityRank(hProcess,nPrioriyRank+1);
if(AURA_FAILED(ec))
{
printf("Error: AuraProcess_SetPriorityRank failed! \
        file:%s. line:%d. \n",
        __FILE__, __LINE__);
}

Aura_SystemTime startTime, exitTime;
ec = AuraProcess_GetLifespan(hProcess,&startTime,&exitTime);
if(AURA_FAILED(ec))
{
printf("Error: AuraProcess_GetLifespan failed! \
        file:%s. line:%d. \n",
        __FILE__, __LINE__);
}



    printf("Kill the child process.\n");
    //getchar();
    ec = AuraProcess_Kill(hProcess, 0);
    if(AURA_FAILED(ec)){
        printf("Error: AuraProcess_Kill failed! \
        file:%s. line:%d. \n",
        __FILE__, __LINE__);
    }

    printf("Get the child process exitcode\n");
    //getchar();
    Aura_ExitCode iExitCode;
    AuraProcess_GetExitCode(hProcess, &iExitCode);
    //DumpProcessInfo((ProcessInfo *)hProcess);

    printf("Clean info and exit.\n");
    //getchar();
    AuraCentralContext_Clear();
    printf("End aura process. \n");
    AuraProcess_Exit(0);
    printf("NOT End aura process. \n");
    return (int)ec;
}










